package com.yunb.service;

import java.util.*;
import java.util.concurrent.CountDownLatch;

import com.alibaba.fastjson.JSONObject;
import com.yunb.entity.Basic;
import com.yunb.util.JedisUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 多线程模拟用户抢红包
 */
@Component
public class GetRedPack {

	@Resource
	private JedisUtils jedis;

    //抢红包的方法
	public void getHongBao() throws InterruptedException {
		final CountDownLatch latch = new CountDownLatch(Basic.threadCount);//20 //发枪器

		for(int i = 0 ; i < Basic.threadCount ; i ++){  //20线程
			Thread thread = new Thread(){
				@Override
				public void run(){
					latch.countDown();
					try {
						latch.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					while(true){
						//模拟一个用户ID使用UUID XXXX
						String userid = UUID.randomUUID().toString();

						//抢红包  eval 可以直接调用我们LUA脚本里的业务代码  object ={rid_1:1, money:9, userid:001}
						Object object = jedis.eval(Basic.getHongBaoScript,4,Basic.hongBaoPoolKey,Basic.hongBaoDetailListKey,Basic.userIdRecordKey,userid);

						if(null != object){
							System.out.println("用户ID号："+userid+" 抢到红包的详情是 "+object);
						}else{
							if(jedis.llen(Basic.hongBaoPoolKey) == 0){
								break;
							}
						}
					}
				}
			};
			thread.start();

		}
	}

 	//自己玩抢红包
	public void playSelfRedWar() throws InterruptedException {
		final CountDownLatch latch = new CountDownLatch(Basic.threadCount);

		List<Map> lmPlay = getMyFamily();

		for(int i = 0 ; i < lmPlay.size() ; i ++){  //20线程
			Map<String,String> map= lmPlay.get(i);
			String sname ="";
			String sid= "";
			for (String key : map.keySet()) {
				sname = map.get(key);
				sid = key;
			}
			String threadName = sname+":"+sid;

			Thread thread = new Thread(threadName){
				private String tsid ="";
				private String tsname ="";
				@Override
				public void run(){
					tsname = threadName.split(":")[0];
					tsid = threadName.split(":")[1];

					latch.countDown();
					try {
						latch.await();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					while(true){
						//模拟一个用户ID使用UUID XXXX
						String userid = tsid;
								//UUID.randomUUID().toString();
						//抢红包  eval 可以直接调用我们LUA脚本里的业务代码  object ={rid_1:1, money:9, userid:001}
						Object object = jedis.eval(Basic.getHongBaoScript,4,Basic.hongBaoPoolKey,Basic.hongBaoDetailListKey,Basic.userIdRecordKey,userid);
						JSONObject jsonObject = JSONObject.parseObject((String) object);
						if(null != object){
							System.out.println("      "+tsname +" 抢到的红包金额： "+jsonObject.get("money"));
						}else{
							if(jedis.llen(Basic.hongBaoPoolKey) == 0){
								break;
							}
						}
					}
				}
			};
			thread.start();

		}
	}

	public List<Map> getMyFamily(){

		List<Map> lm = new ArrayList<Map>();

		Map<String,String> m2 = new HashMap();
		m2.put("08","瑶瑶");
		Map<String,String> m3 = new HashMap();
		m3.put("03","永红");
		Map<String,String> m4 = new HashMap();
		m4.put("01","瑞瑞");
		Map<String,String> m5 = new HashMap();
		m5.put("04","瑞雪");
		Map<String,String> m8 = new HashMap();
		m8.put("06","姚博");
		Map<String,String> m6 = new HashMap();
		m6.put("10","爸");
		Map<String,String> m7 = new HashMap();
		m7.put("05","妈");
		Map<String,String> m1 = new HashMap();
		m1.put("07","飞龙");

		lm.add(m2);
		lm.add(m3);
		lm.add(m4);
		lm.add(m5);
		lm.add(m8);
		lm.add(m6);
		lm.add(m7);
		lm.add(m1);

		return lm;
	}
}
